#!/bin/bash

LOG_FILE="/var/log/deploy.log"

echo "------ 部署开始：$(date) ------" >> "$LOG_FILE"
cd /var/www/habit-koa || {
  echo "❌ 无法进入项目目录" >> "$LOG_FILE"
  exit 1
}

git reset --hard >> "$LOG_FILE" 2>&1
git pull origin master >> "$LOG_FILE" 2>&1 || {
  echo "❌ Git 拉取失败" >> "$LOG_FILE"
  exit 1
}

npm install >> "$LOG_FILE" 2>&1 || {
  echo "❌ 依赖安装失败" >> "$LOG_FILE"
  exit 1
}

npx prisma generate

pm2 reload habit >> "$LOG_FILE" 2>&1
if [ $? -ne 0 ]; then
  echo "❌ PM2 重启失败" >> "$LOG_FILE"
else
  echo "🚀 PM2 重启成功" >> "$LOG_FILE"
fi

echo "✅ 部署完成：$(date)" >> "$LOG_FILE"
